package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.DeleteLeaveByIdForm;
import com.example.emos.api.controller.form.InsertLeaveForm;
import com.example.emos.api.controller.form.SearchLeaveByIdForm;
import com.example.emos.api.controller.form.SearchLeaveByPageForm;
import com.example.emos.api.db.pojo.TbLeave;
import com.example.emos.api.service.LeaveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

@RestController
@RequestMapping("/leave")
@Tag(name = "LeaveController", description = "员工请假Web接口")
public class LeaveController {
    @Autowired
    private LeaveService leaveService;

    @PostMapping("/searchLeaveByPage")
    @Operation(summary = "查询请假分页数据")
    @SaCheckLogin
    public R searchLeaveByPage(@Valid @RequestBody SearchLeaveByPageForm form) {
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
        param.put("myId",StpUtil.getLoginIdAsInt());
        if (!(StpUtil.hasPermission("LEAVE:SELECT") || StpUtil.hasPermission("ROOT"))) {
            param.put("userId",StpUtil.getLoginIdAsInt());
        }
        PageUtils pageUtils = leaveService.searchLeaveByPage(param);
        return R.ok().put("page",pageUtils);
    }

    @PostMapping("/insert")
    @Operation(summary = "添加请假记录")
    @SaCheckLogin
    public R insert(@Valid @RequestBody InsertLeaveForm form) {
        // 判断结束时间是不是早于开始时间
        DateTime date_start = DateUtil.parse(form.getStart());
        DateTime date_end = DateUtil.parse(form.getEnd());
        if (date_start.isAfterOrEquals(date_end)) {
            return R.error("请假 startTime 不能晚于/等于 endTime");
        }

        // 判断当前请假区间是否跟其他请假区间有交集
        HashMap param = new HashMap();
        param.put("userId", StpUtil.getLoginIdAsInt());
        param.put("start", form.getStart());
        param.put("end", form.getEnd());
        boolean bool = leaveService.searchContradiction(param);
        if (bool == true) {
            return R.error("当前请假申请日期区间与已有请假申请日期区间上有交集");
        }

        // 计算请假天数 days
        long hours = date_start.between(date_end, DateUnit.HOUR);
        // divide() 的第二个参数表示最后结果保留几位小数，例如 3 / 24 = 0.125 CEILING 并保留 1 位小数后为 0.2
        String days = new BigDecimal(hours).divide(new BigDecimal(24), 1, RoundingMode.CEILING).toString();
        if (days.contains(".0")) {
            days = days.replace(".0", "");
        }
        if (days.equals("0")) {
            days = "0.1";
        }

        // 添加请假记录
        TbLeave leave = JSONUtil.parse(form).toBean(TbLeave.class);
        leave.setUserId(StpUtil.getLoginIdAsInt());
        leave.setDays(days);

        int rows = leaveService.insert(leave);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/deleteLeaveById")
    @Operation(summary = "删除请假记录")
    @SaCheckLogin
    public R deleteLeaveById(@Valid @RequestBody DeleteLeaveByIdForm form) {
        HashMap param = new HashMap();
        param.put("id", form.getId());
        param.put("userId", StpUtil.getLoginIdAsInt());
        // 删除请假记录
        int rows = leaveService.deleteLeaveById(param);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/searchLeaveById")
    @Operation(summary = "根据Id查询请假数据")
    @SaCheckLogin
    public R searchLeaveById(@Valid @RequestBody SearchLeaveByIdForm form) {
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        if (!(StpUtil.hasPermission("LEAVE:SELECT") || StpUtil.hasPermission("ROOT"))) {
            param.put("userId",StpUtil.getLoginIdAsInt());
        }
        HashMap map = leaveService.searchLeaveById(param);
        return R.ok(map);
    }
}
